package com.ziroom.crm.mq.rabbit.spring.resolve;

import com.alibaba.fastjson.JSON;
import com.ziroom.crm.mq.core.MqType;
import com.ziroom.crm.mq.core.config.MqConfig;
import com.ziroom.crm.mq.rabbit.spring.data.MqRabbitConst;
import com.ziroom.crm.mq.rabbit.spring.data.RabbitMessage;
import com.ziroom.crm.mq.rabbit.spring.wrapper.RabbitConsumeQueueWrapper;
import com.ziroom.crm.mq.rabbit.spring.wrapper.RabbitSendQueueWrapper;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;

import java.nio.charset.Charset;
import java.util.Objects;
import java.util.UUID;

/**
 * <p></p>
 * <p>
 * <PRE>
 * <BR>    修改记录
 * <BR>-----------------------------------------------
 * <BR>    修改日期         修改人          修改内容
 * </PRE>
 *
 * @author WeiShaoying
 * @version 1.0
 * @date Created in 2020年09月01日 15:57
 * @since 1.0
 */
@SuppressWarnings("ALL")
public class RabbitResolve {

    /**
     * 解析为QueueWrapper
     * 发送队列
     *
     * @param rabbitMessage
     * @return
     */
    public static RabbitSendQueueWrapper buildSendQueueWrapper(RabbitMessage rabbitMessage) {
        return new RabbitSendQueueWrapper(MqType.RABBIT, rabbitMessage.getExchange(), rabbitMessage.getRoutingKey());
    }

    /**
     * 解析为QueueWrapper
     *
     * @param rabbitMessage
     * @return
     */
    public static RabbitConsumeQueueWrapper buildConsumeQueueWrapper(RabbitMessage rabbitMessage) {
        return new RabbitConsumeQueueWrapper(MqType.RABBIT, rabbitMessage.getQueueName());
    }

    /**
     * 解析为QueueWrapper
     *
     * @param queueName
     * @return
     */
    public static RabbitConsumeQueueWrapper buildConsumeQueueWrapper(String queueName) {
        return new RabbitConsumeQueueWrapper(MqType.RABBIT, queueName);
    }

    /**
     * Message 转 RabbitMessage
     *
     * @param message
     * @return 自定义
     */
    public static RabbitMessage convert(Message message) {
        String id = UUID.randomUUID().toString();
        String messageBody = getBodyStr(message);

        // 不是重试消息体，初始化
        MessageProperties messageProperties = message.getMessageProperties();
        String messagePropertiesStr = getPropertiesStr(messageProperties);

        return RabbitMessage.builder()
                .id(id)
                .messageProperties(messagePropertiesStr)
                .messageBody(messageBody)
                .build();
    }

    /**
     * template 的 MessageConverter 势必要 转json
     */
    private static String getBodyStr(Message message) {
        return new String(message.getBody(), Charset.forName(MqConfig.charset()));
    }

    private static String getPropertiesStr(MessageProperties messageProperties) {
        if (Objects.isNull(messageProperties)) {
            return null;
        }
        // 自定义序列化
        return JSON.toJSONString(messageProperties);
    }

    /**
     * 判断是否来源于重试的消息体
     *
     * @param message 消息体
     * @return boolean
     */
    public static boolean isResendMessage(Message message) {
        return message.getMessageProperties().getHeaders().containsKey(MqRabbitConst.MQ_RESEND_RETRY_COUNT);
    }

    public static void setResendCount(Message message, Integer count) {
        count = Objects.isNull(count) ? 0 : count;
        message.getMessageProperties().setHeader(MqRabbitConst.MQ_RESEND_RETRY_COUNT, count);
    }

    public static void setReconsumeCount(Message message, Integer count) {
        count = Objects.isNull(count) ? 0 : count;
        message.getMessageProperties().setHeader(MqRabbitConst.MQ_RECONSUME_RETRY_COUNT, count);
    }

}
